//
//  goodsDetailViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/9.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "goodsDetailViewController.h"
#import "Macros.h"
#import "yiYuanDuoBaoNextView.h"
#import "yiYuanDuoBaoViewController.h"
#import "tuWenXiangQingViewController.h"
#import "HttpRequest.h"
#import "wangQiJieXiaoViewController.h"
#import "yiYuanDuoBao.h"
#import "goodsDetail.h"
#import "UIImageView+WebCache.h"
#import "newYiYuanDuoBaoViewController.h"
#import "fenLeiController.h"
#import "shaiDan.h"
#import "shaiDanViewController.h"
#import "TCLYUser.h"
#import "orderFinishViewController.h"
#import "orderFinish.h"

#import "SVProgressHUD.h"
//跳到购买页面
#import "activityOrderViewController.h"
@interface goodsDetailViewController ()
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *picImageView;
@property (weak, nonatomic) IBOutlet UILabel *goodsLabel;
@property (weak, nonatomic) IBOutlet UIView *grayView;
@property (weak, nonatomic) IBOutlet UILabel *involveLabel;
@property (weak, nonatomic) IBOutlet UILabel *sumLabel;
@property (weak, nonatomic) IBOutlet UIView *lastView;
@property (weak, nonatomic) IBOutlet UIButton *joinButton;
@property (weak, nonatomic) IBOutlet UIView *line01;
@property (weak, nonatomic) IBOutlet UIView *line02;
@property (weak, nonatomic) IBOutlet UIView *line03;
@property (weak, nonatomic) IBOutlet UILabel *muQianRenShuLabel;
@property (weak, nonatomic) IBOutlet UILabel *gongXu;
@property(nonatomic,copy)NSMutableArray *dataArray;
@property(nonatomic,copy)NSMutableDictionary *dateArray;

@property(nonatomic,retain)UIView *backView;
@end

@implementation goodsDetailViewController
@synthesize dataArray,dateArray;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    dataArray=[NSMutableArray array];
    dateArray=[NSMutableDictionary dictionary];
    //设置背景色
    [self setBackGroundColor];
    [self getDataFromNet];
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    self.backView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight-64)];
    self.backView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.backView];
}
//设置背景色
-(void)setBackGroundColor
{
//    216 26 96
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.involveLabel.textColor=pinkColor;
    self.sumLabel.textColor=pinkColor;
    self.line01.backgroundColor=customGrayColor;
    self.line02.backgroundColor=customGrayColor;
    self.line03.backgroundColor=customGrayColor;
    self.lastView.backgroundColor=customGrayColor;
    self.joinButton.backgroundColor=[UIColor colorWithRed:216/255.0 green:26/255.0 blue:96/255.0 alpha:1];
    self.muQianRenShuLabel.textColor=pinkColor;
    self.gongXu.textColor=pinkColor;
}
//从服务器获取数据
-(void)getDataFromNet
{
    yiYuanDuoBao *yydb=[yiYuanDuoBao shardeManager];
    NSString *idString=yydb.idString;
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/indiana/detail"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"id"]=idString;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         NSLog(@"json=%@",json);
         dataArray=[json objectForKey:@"data"];
         if (dataArray.count>0)
         {
             [self dismiss];
         }
//         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    [self.backView removeFromSuperview];
    [SVProgressHUD dismiss];
    [self setBackGroundContent];
}
//设置背景
-(void)setBackGroundContent
{
    goodsDetail *gd=[goodsDetail shardeManager];
    gd.sumString=[dataArray[0] objectForKey:@"sum"];
    gd.contentString=[dataArray[0] objectForKey:@"content"];
    gd.idString=[dataArray[0] objectForKey:@"id"];
    gd.involeString=[dataArray[0] objectForKey:@"invole"];
    gd.nameString=[dataArray[0] objectForKey:@"name"];
    gd.picString=[dataArray[0] objectForKey:@"pic"];
    //图像
    NSURL* imagePath = [NSURL URLWithString:gd.picString];
    [self.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    self.goodsLabel.text=gd.contentString;
    self.sumLabel.text=gd.sumString;
    self.involveLabel.text=gd.involeString;
    float involve=[gd.involeString intValue];
    float sum=[gd.sumString intValue];
//    NSLog(@"involve/sum=%f",involve/sum);
    float redView_width=(involve/sum)*self.grayView.frame.size.width;
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(self.grayView.frame.origin.x, self.grayView.frame.origin.y, redView_width, self.grayView.frame.size.height)];
    redView.backgroundColor=pinkColor;
    [self.view addSubview:redView];
}
//返回按钮
- (IBAction)goBack:(id)sender
{
    yiYuanDuoBaoNextView *yydbn=[yiYuanDuoBaoNextView shardeManager];
    if (yydbn.whichViewToNext==0)
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else if (yydbn.whichViewToNext==1)
    {
        yiYuanDuoBaoViewController *yydb=self.navigationController.viewControllers[1];
        [self.navigationController popToViewController:yydb animated:YES];
    }
    else if (yydbn.whichViewToNext==3)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[newYiYuanDuoBaoViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
    else if (yydbn.whichViewToNext==4)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[fenLeiController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
}
//图文详情
- (IBAction)tuWenXiangQing:(id)sender
{
    tuWenXiangQingViewController *twxq=[tuWenXiangQingViewController new];
    [self.navigationController pushViewController:twxq animated:YES];
}

//晒单分享
- (IBAction)shaiDanFenXiang:(id)sender
{
    shaiDan *sd=[shaiDan shardeManager];
    sd.whichViewToBack=2;
    shaiDanViewController *sdvc=[shaiDanViewController new];
    [self.navigationController pushViewController:sdvc animated:YES];
}
//往期揭晓
- (IBAction)wangQiJieXiao:(id)sender
{
    wangQiJieXiaoViewController *wqjx=[wangQiJieXiaoViewController new];
    [self.navigationController pushViewController:wqjx animated:YES];
}
//立即参与
- (IBAction)joinBtn:(id)sender
{
    goodsDetail *gd=[goodsDetail shardeManager];
    NSLog(@"gd.num=%d",gd.num);
    if (gd.num==0)
    {
        [self getNextDataFromNet];

    }
    else
    {
    
        NSLog(@"花钱夺宝");
        activityOrderViewController *aovc=[activityOrderViewController new];
        [self.navigationController pushViewController:aovc animated:YES];
    }
    
}
-(void)getNextDataFromNet
{
    yiYuanDuoBao *yydb=[yiYuanDuoBao shardeManager];
    NSString *idString=yydb.idString;
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/indiana/order"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"tid"]=idString;
    parameters[@"uid"]=userID;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //1 可以跳转。0不可以跳转
//         NSLog(@"json=%@",json);
         dateArray=[json objectForKey:@"data"];
         NSString *str=[dateArray objectForKey:@"have"];
         int num=[str intValue];
//         NSLog(@"");
         if (num==0)
         {
             [self createBuyThing];
         }
         else
         {
             orderFinish *of=[orderFinish sharedManager];
             of.isTrue=1;
             of.moneyString=[dateArray objectForKey:@"num"];
             of.timeString=[dateArray objectForKey:@"time"];
             //订单成功的界面
             orderFinishViewController *ovc=[orderFinishViewController new];
             [self.navigationController pushViewController:ovc animated:YES];
         }
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
-(void)createBuyThing
{
    UIAlertController * alertController=[UIAlertController alertControllerWithTitle:nil message:@"您已经参与过免费夺宝" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"谢谢" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        nil;
    }];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
